/*
 * Copyright (c) 2010 Network Security Lab (NSL), Department of Electrical
 *                    Engineering, University of Washington
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: He Wu mdzz@uw.edu
 */

/**
 * @file router.h
 * @brief Multi-hop routing implementation.
 */

#ifndef ROUTER_H
#define ROUTER_H

#include <time.h>
#include <stdlib.h>
#include "network.h"
#include "util.h"

// multi-hop router parameters
#define ROUTING_BEACON_INTERVAL 5000000 //!< routing broadcast interval (in us)
#define ROUTING_TABLE_SIZE 4	//!< size of to base routing table (max 11)
#define ROUTING_DATA_SIZE 26	//!< ROUTING_DATA_SIZE = ROUTING_TABLE_SIZE * 5 + 6
#define MAX_STAY_TO_BASE 30   //!< maximum time an entry stays in routing table
#define MAX_STAY_TO_NODE 50		//!< maximum time an entry stays in to-node routing table

// other parameters
#define MAX_NUM_NODES_IN_RT 7	//!< maximum number of entries in routing table
#define TO_BASE_ROUTING_RESEND 1	//!< # of re-sends for to base routing packet
#define TO_NODE_ROUTING_RESEND 1	//!< # of re-sends for to node routing packet
#define TABLE_DISPLAY_INTERVAL 5  //!< display and report routing table to GUI every TABLE_DISPLAY sec

/******************************************************************************/

/**
 * Node property
 */
typedef struct nodeProperty_st
{
	__u16 addr;    //!< local address
	__u8 numHops;  //!< # of hops to base
} nodeProperty;

/**
 * Node-to-base routing table entry
 */
typedef struct toBaseRoutingEntry_st
{
	__u16 nextHop; //!< address for next hop
	__u8 numHops;  //!< # of hops to base
	int lastHeard; //!< when is it last updated
} toBaseRoutingEntry;

/**
 * Base-to-node routing table entry
 */
typedef struct toNodeRtEntry_st {
  __u8 length;      //!< size of the address list (including the destination node)
  __u16 destAddr;   //!< address of destination node
  __u16 *addrList;  //!< list of addresses to get to destination node
  int age;          //!< when is it last updated
  struct toNodeRtEntry_st *next;  //!< pointer to next entry
} toNodeRtEntry;

typedef struct toNodeRtTable_st {
  toNodeRtEntry *head;
  int size;
  pthread_mutex_t lock;
} toNodeRtTable;


/******************************************************************************/

/**
 * Initializes the routing tables. Not thread safe, but should be alright since
 * this function is to be called once and the beginning of main program.
 */
void initTable();

/**
 * Sends out routing beacon every ROUTING_BEACON_INTERVAL seconds.
 */
void routingBeacon();

/**
 * @brief Process node-to-base routing packets.
 * @param in_pkt Pointer to routing packet received.
 */
void recv_rtPkt(packet *in_pkt);

/**
 * @brief Sends packets back to base node using routing information.
 * @param out_pkt Pointer to packet to be sent.
 * @param ACK_en Set to enable ACK.
 * @return 0 if all is good
 *
 * Currently packets are always sent to the first destination in routing table.
 */
int sendtoBase(packet *out_pkt, int ACK_en);

/**
 * @brief Sends command packet to node using base-to-node routing information.
 * @param destAddr Destination address.
 * @param out_pkt Pointer to packet to be sent.
 * @param ACK_en Set to enable ACK.
 * @return 0 if all is good.
 *
 * The actual command packet data is embedded into the last 9 bytes of the data
 * payload. # of hops left is recorded in the first byte of the data payload.
 * Every time the packet passes through a node, this number is reduced by 1.
 */
int sendtoNode(__u16 destAddr, packet *out_pkt, int ACK_en);

/**
 * @brief Process and forward base-to-node routing packets using node-to-base
 * routing information.
 * @param out_pkt
 * @return 0 if all is good.
 */
int forward_rt_toBase(packet *out_pkt);

/**
 * Display the routing table periodically. Also reports the base-to-node routing
 * table to GUI at the base node.
 */
void displayRoutingTable();

#endif
